package com.baor.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baor.dao.Equipment_abnormalityDao;
import com.baor.domain.Equipment_abnormality;
import com.baor.service.ExceptionReportService;
import com.baor.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ExceptionReportServiceImpl implements ExceptionReportService {

    @Autowired
    private Equipment_abnormalityDao equipment_abnormalityDao;
    @Override
    public R update( Map<String, Object> params) {
        // 参数校验
        if (params == null || params.isEmpty()) {
            return R.er("参数不能为空");
        }

        // 检查必需参数
        if (!params.containsKey("id") || params.get("id") == null) {
            return R.er("ID不能为空");
        }

        try {
            // 构建更新条件
            LambdaUpdateWrapper<Equipment_abnormality> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Equipment_abnormality::getId, params.get("id"));

            if (params.containsKey("material") && params.get("material") != null) {
                Object materialValue = params.get("material");
                if (materialValue instanceof List) {
                    // 将 List 转换为逗号分隔的字符串
                    List<?> materialList = (List<?>) materialValue;
                    String materialString = String.join(",", materialList.stream()
                            .map(Object::toString)
                            .collect(Collectors.toList()));
                    wrapper.set(Equipment_abnormality::getMaterial, materialString);
                } else if (materialValue instanceof String) {
                    wrapper.set(Equipment_abnormality::getMaterial, materialValue);
                }
            }
            if (params.containsKey("abnormalDescription") && params.get("abnormalDescription") != null) {
                wrapper.set(Equipment_abnormality::getAbnormalDescription, params.get("abnormalDescription"));
            }
            if (params.containsKey("result") && params.get("result") != null) {
                wrapper.set(Equipment_abnormality::getResult, params.get("result"));
            }

            // 添加更新时间
            wrapper.set(Equipment_abnormality::getUpdatedAt, LocalDateTime.now());

            int updateCount = equipment_abnormalityDao.update(null, wrapper);

            if (updateCount > 0) {
                return R.ok("更新成功");
            } else {
                return R.er("更新失败，记录不存在或数据无变化");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.er("更新异常: " + e.getMessage());
        }
    }

    //TODO 异常上报
    @Override
    public R insert(Map<String, Object> params) {
        Equipment_abnormality equipmentReport = new Equipment_abnormality();
        // 设置上报用户ID
        if (params.containsKey("reportUserId")) {
            equipmentReport.setReportUserId(Integer.valueOf(params.get("reportUserId").toString()));

        } else {
            return R.er("上报用户不能为空");
        }
        // 设置设备ID
        if (params.containsKey("equipmentId")) {
            equipmentReport.setEquipmentId((Integer) params.get("equipmentId"));
        } else {
            return R.er("设备不能为空");
        }
        // 设置异常类型和异常描述
        if (params.containsKey("abnormalType")) {
            equipmentReport.setAbnormalType(params.get("abnormalType").toString());
        } else {
            return R.er("异常类型不能为空");
        }

        if (params.containsKey("abnormalDescription")) {
            equipmentReport.setAbnormalDescription(params.get("abnormalDescription").toString());
        } else {
            return R.er("异常描述不能为空");
        }

        // 设置上报时间为当前时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        equipmentReport.setReportTime(LocalDateTime.now().format(formatter));

        // 设置创建时间和更新时间
        equipmentReport.setCreatedAt(LocalDateTime.now().format(formatter));
        equipmentReport.setUpdatedAt(LocalDateTime.now().format(formatter));

        equipmentReport.setReportStatus(0);

        System.out.println("equipmentReport:"+equipmentReport);
        // 使用 MyBatis Plus 保存到数据库
        int result = equipment_abnormalityDao.insert(equipmentReport);

        System.out.println("result:"+result);
        if (result > 0) {
            return R.ok("异常上报成功");
        } else {
            return R.er("异常上报失败");
        }
    }
}
